local skel = fk.CreateSkill {
  name = "lb__xunji",
}

Fk:loadTranslationTable {
  ["lb__xunji"] = "寻己",
  [":lb__xunji"] = "每回合各限一次，当你脱离濒死或体力值变化后，你可以<a href='#lb__xunji_js'><font color='red'>与自己拼点</font></a>，若点数相差大于X，你展示所有手牌，摸X张牌并令<a href=':lb__lihuan'><font color='red'>〖离焕〗</font></a>失效至你的下个回合开始（X为已损失体力值）。",

  ["#lb__xunji_js"] = "将两张手牌置入弃牌堆",
  ["#lb__xunji"] = "寻己：你可以将两张手牌置入弃牌堆",
  ["#lb__xunji-nil"] = "失效",

  ["$lb__xunji1"] = "前尘旧梦，俱已往矣。",
  ["$lb__xunji2"] = "蜕变，总免不了挣脱过去。",
}

local xunji_sepc = {
  max_turn_use_time = 1,
  anim_type = "masochism",
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(skel.name) and target == player and player:getHandcardNum() > 1 
    --and skel:withinBranchTimesLimit(player, (event == fk.AfterDying and "AfterDying" or "HpChanged"), Player.HistoryTurn)
  end,
  on_cost = function(self, event, target, player, data)
    local cards = player.room:askToCards(player, {
      min_num = 2,
      max_num = 2,
      include_equip = false,
      skill_name = "lb__xunji",
      cancelable = true,
      prompt = "#lb__xunji",
    })
    event:setCostData(self, { cards = cards })
    return #cards > 1
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local cards = event:getCostData(self).cards
    room:throwCard(cards, skel.name, player, player)
    local n = Fk:getCardById(cards[1]).number - Fk:getCardById(cards[2]).number
    local m = (player.maxHp - player.hp)
    if math.abs(n) > m then
      if player:getHandcardNum() > 0 then
        player:showCards(player:getCardIds("h"))
      end
      if player:hasSkill("lb__lihuan") then
        room:invalidateSkill(player, "lb__lihuan")
        room:setPlayerMark(target, "@lb__lihuan", "#lb__xunji-nil")
      end
      player:drawCards(m, skel.name)
    end
  end,
}

skel:addEffect(fk.HpChanged, xunji_sepc, { check_effect_limit = true })
skel:addEffect(fk.AfterDying, xunji_sepc, { check_effect_limit = true})

skel:addEffect(fk.TurnStart, {
  can_refresh = function(self, event, target, player, data)
    return target == player and player:getMark("@lb__lihuan") ~= 0
  end,
  on_refresh = function(self, event, target, player, data)
    player.room:setPlayerMark(player, "@lb__lihuan", 0)
    player.room:validateSkill(player, "lb__lihuan")
  end,
})

return skel
